<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/11/14
 * Time: 14:41
 * Created by Created by Panxsoft.
 */

namespace App\Models\Products;

use App\Models\Options\Option;
use App\Models\Options\OptionDescription;
use App\Models\Options\OptionValue;
use App\Models\Options\OptionValueDescription;
use App\Services\Locale\Language;
use App\Services\MobileApp\AppEnv;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class ProductAttribute
 * @package App\Models\Products
 * @mixin Builder
 */
class ProductOptionValue extends Model
{
    const PRODUCT_OPTION_VALUE_ID_DEFAULT = 0;
    const PRODUCT_OPTION_VALUE_SUBTRACT = 1; // 是否减扣，1是0否
    const PRODUCT_OPTION_VALUE_NO_SUBTRACT = 0; // 是否减扣，1是0否

    protected $guarded = [];

    protected $table = 'product_option_value';

    protected $primaryKey = 'product_option_value_id';

    public $timestamps = false;

    public $casts = [
        'subtract' => 'int'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function optionValueDescription()
    {
        return $this->belongsTo(OptionValueDescription::class,'option_value_id','option_value_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function optionDescription()
    {
        return $this->belongsTo(OptionDescription::class,'option_id','option_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function optionByType()
    {
        return $this->belongsTo(Option::class,'option_id','option_id')
            ->where('type', Option::DEFAULT_OPTION_TYPE);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function optionType()
    {
        return $this->belongsTo(Option::class,'option_id','option_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function productOption()
    {
        return $this->belongsTo(ProductOption::class,'product_option_id','product_option_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function optionValue()
    {
        return $this->belongsTo(OptionValue::class, 'option_value_id', 'option_value_id');
    }

    /**
     * @param $product_option_value_id
     * @return ProductOptionValue|Model|null|object
     */
    public static function getProductOptionValue($product_option_value_id)
    {
        $product_option_value = self::where('product_option_value_id', $product_option_value_id)
            ->first();

        return $product_option_value;
    }
}